package com.foodmarket.controller;

import com.foodmarket.pojo.FoodItems;
import com.foodmarket.pojo.Result;
import com.foodmarket.service.FoodService;
import com.foodmarket.utils.ThreadLocalUtil;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.logging.Logger;

@RestController
@RequestMapping("/food")
public class FoodController {

    @Autowired
    private FoodService foodService;

    @PostMapping("addFood")
    public Result addFood(@RequestBody FoodItems foodItems)
    {
        foodService.add(foodItems);
        return Result.success();
    }

    @GetMapping("/foodInfo")//查询
    public Result<FoodItems> foodInfo( ){
        //根据食品名称查询
        Map<String,Object> map = ThreadLocalUtil.get();
        String foodName = (String) map.get("foodName");
        FoodItems foods= foodService.findByFoodName(foodName);
        return  Result.success(foods);
    }

    @GetMapping("/allFoodInfo")//查询
    public  Result <List<FoodItems>> allFoodInfo( ){
        //根据当前用户id查询
        Map<String,Object> map=ThreadLocalUtil.get();
        System.out.println(map);

        int SellerId=(int) map.get("id");
        List<FoodItems> foods= foodService.findAllFood(SellerId);
        return  Result.success(foods);
    }

    @PutMapping("/updateFood")//更新
    public Result updateFood(@RequestBody @Validated FoodItems foodItems){
        foodService.updateFood(foodItems);
        return Result.success();
    }

    @DeleteMapping("/deleteFood")//删除
    public Result deleteByName(){
        Map<String,Object> map = ThreadLocalUtil.get();
        String foodName = (String) map.get("foodName");
        foodService.deleteByFoodName(foodName);
        return Result.success();
    }

    @GetMapping("/allFoodItems")
    public Result<List<FoodItems>> findAllFoodItems()
    {
        List<FoodItems> foods= foodService.findAllFoodItems();
        return  Result.success(foods);
    }

    @GetMapping("/FoodInfo")//查询
    public  Result  FoodInfo(@RequestParam int foodId){
        System.out.println(foodId+"123");
        FoodItems foods= foodService.findByFoodId(foodId);
        return  Result.success(foods);
    }


}
